package com.infosystem.demo.controllers;

import com.infosystem.demo.helpers.DepartmentListEnum;
import com.infosystem.demo.entities.Employee;
import com.infosystem.demo.helpers.JobTitleEnum;
import com.infosystem.demo.helpers.PoliticalTitleEnum;
import com.infosystem.demo.helpers.RelativeTypeEnum;
import com.infosystem.demo.models.*;
import com.infosystem.demo.services.EmployeeServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Date;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/api/v1/employee")
public class EmployeeController {
    @Autowired
    private EmployeeServices employeeServices;

    // add employee
    @PostMapping("/add")
    public ResponseEntity<Employee> addEmployee(@RequestParam String name, @RequestParam String gov_id, @RequestParam DepartmentListEnum department, @RequestParam Date enroll_date ) throws ParseException, SQLException {
        EmployeeDTOI employeeDTOI = new EmployeeDTOI(name, gov_id, department, enroll_date);
        Employee employee = new Employee(employeeDTOI);
        Employee employeeReturned = employeeServices.addEmployee(employee);
//        HashMap<Object, Object> result = new HashMap<>();
//        result.put("code",200);
//        result.put("msg","12121");
//        result.put("data",employee1);
        return ResponseEntity.status(HttpStatus.OK).body(employeeReturned);
    }

    //用id检索
    @GetMapping("/getById/{id}")
    public EmployeeDTOO getEmployeeById(@PathVariable String id) throws SQLException {
        return employeeServices.getEmployeeFullInfoById(id);
    }

    //用名字检索
    @GetMapping("/getByName/{employee_name}")
    public List<EmployeeDTOO> getEmployeeByName(@PathVariable String employee_name) throws SQLException {
        return employeeServices.getEmployeeByName(employee_name);
    }

    //用部门检索

    //用日期检索
    @GetMapping("/getByEnrollDate/{enroll_date}")
    public List<EmployeeDTOO> getEmployeeByEnrollDate(@PathVariable Date enroll_date) throws SQLException {
        return employeeServices.getEmployeeByEnrollDate(enroll_date);
    }
    @GetMapping("/getByAgeRange")
    public List<EmployeeDTOO> getEmployeeByAgeRange(@RequestParam int start_age, @RequestParam int end_age) throws SQLException {
        return employeeServices.getEmployeeByAgeRange(start_age, end_age);
    }
    //fuzzy search
    @GetMapping("/getByFuzzyName/{employee_name}")
    public List<EmployeeDTOO> getEmployeeByFuzzyName(@PathVariable String employee_name) throws SQLException {
        return employeeServices.getEmployeeByFuzzyName(employee_name);
    }
    @GetMapping("/getByFuzzyGovId/{gov_id}")
    public List<EmployeeDTOO> getEmployeeByFuzzyGovId(@PathVariable String gov_id) throws SQLException {
        return employeeServices.getEmployeeByFuzzyGovId(gov_id);
    }
    // update employee by id
    @PostMapping("/update")
    public void updateEmployee(@RequestParam String id, @RequestParam String name, @RequestParam String gov_id, @RequestParam DepartmentListEnum department, @RequestParam Date enroll_date) throws SQLException {
        EmployeeDTOI employeeDTOI = new EmployeeDTOI(name, gov_id, department, enroll_date);
        employeeServices.updateEmployee(id, employeeDTOI);
    }
    // delete employee by id
    @GetMapping("/delete/{id}")
    public void deleteEmployee(@PathVariable String id) throws SQLException {
        employeeServices.deleteEmployee(id);
    }
    // batch delete employee by id, json list
    @PostMapping("/batchDelete")
    public void deleteEmployee(@PathVariable List<String> ids) throws SQLException {
        employeeServices.batchDeleteEmployee(ids);
    }
    /********************* relative controller *********************/
    @PostMapping("/addRelative")
    public void addRelative(@RequestParam String employee_id, @RequestParam String name, @RequestParam JobTitleEnum job_title, @RequestParam PoliticalTitleEnum political_title, @RequestParam RelativeTypeEnum type) throws SQLException {
        RelativeDTOI relativeDTOI = new RelativeDTOI(employee_id, name, job_title, political_title, type);
        employeeServices.addRelative(relativeDTOI);
    }

    @GetMapping("/getRelatives/{employee_id}")
    public List<RelativeDTOO> getRelatives(String employee_id) {
        return employeeServices.getRelativeByEmployeeId(employee_id);
    }
    @GetMapping("/getRelativeByJobTitle/{job_title}")
    public List<RelativeDTOO> getRelativeByJobTitle(JobTitleEnum job_title) {
        return employeeServices.getRelativeByJobTitle(job_title.getTitle());
    }
    @GetMapping("/getRelativeByPoliticalTitle/{political_title}")
    public List<RelativeDTOO> getRelativeByPoliticalTitle(PoliticalTitleEnum political_title) {
        return employeeServices.getRelativeByPoliticalTitle(political_title.getTitle());
    }
    @GetMapping("/getRelativeByFuzzyName/{name}")
    public List<RelativeDTOO> getRelativeByFuzzyName(String name) {
        return employeeServices.getRelativeByFuzzyName(name);
    }
    @PostMapping("/deleteRelativeById")
    public void deleteRelativeById(@RequestParam String id) {
        employeeServices.deleteRelativeById(id);
    }
    @PostMapping("/batchDeleteRelative")
    public void batchDeleteRelative(@RequestParam List<String> ids) {
        employeeServices.batchDeleteRelative(ids);
    }

    @PostMapping("/addResume")
    public void addResume(@RequestParam String employee_id, MultipartFile resume){
        ResumeDTOI resumeDTOI = new ResumeDTOI(employee_id, resume);
        employeeServices.uploadResume(employee_id, resumeDTOI);
    }
    @PostMapping("/updateResume")
    public void updateResume(@RequestParam String employee_id, MultipartFile resume){
        ResumeDTOI resumeDTOI = new ResumeDTOI(employee_id, resume);
        employeeServices.uploadResume(employee_id, resumeDTOI);
    }

    @PostMapping("/deleteResume")
    public void deleteResume(@RequestParam String employee_id){
        employeeServices.deleteResume(employee_id);
    }
    @GetMapping("/getResumeById/{employee_id}")
    public ResumeDTOO getResumeById(@PathVariable String employee_id) {
        return employeeServices.getResumeByEmployeeId(employee_id);
    }
}
